package edu.ustb.efws.controller.configure;

import edu.ustb.efws.controller.interceptor.AuthorizationInterceptor;
import edu.ustb.efws.security.authenticate.AuthenticateService;
import edu.ustb.efws.security.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Api配置
 *
 * @author TS Group
 */
@Configuration
public class ControllerConfigure implements WebMvcConfigurer {
    private final TokenService tokenService;

    @Autowired
    public ControllerConfigure(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthorizationInterceptor(tokenService))
                .addPathPatterns("/**")
                .excludePathPatterns("/main/*", "/error", "/doc.html", "/webjars/**", "/swagger-resources/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .maxAge(60 * 60L)
                .allowCredentials(false);
    }
}